package com.journey.org.ui.video

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.FragmentVideoBinding
import com.journey.org.ui.search.gank.GankSearchFragment
import com.journey.org.ui.video.movie.MovieFragment
import com.journey.org.ui.video.recommend.RecommendFragment
import com.journey.org.ui.video.small.SmallVideoFragment
import com.mvvmhabit.org.base.BaseFragment
import com.mvvmhabit.org.base.BaseFragmentPagerAdapter
import com.mvvmhabit.org.base.BaseLazyFragment
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerManager

/**
 * @Date: 2020/11/17 14:24
 * @Author:小妖
 * @Description: 视频
 */
class VideoFragment : BaseLazyFragment<FragmentVideoBinding, VideoViewModel>() {

    override fun lazyLoadData() {
    }

    private val mFragment = ArrayList<Fragment>()

    private val mTitle = ArrayList<String>()

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<VideoViewModel> = VideoViewModel::class.java
    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.fragment_video


    override fun initData() {
        super.initData()
        mFragment.add(RecommendFragment())
        mTitle.add("推荐")

        mFragment.add(MovieFragment())
        mTitle.add("影视资源")

        mFragment.add(SmallVideoFragment())
        mTitle.add("小视频")
        initAdapter()
    }

    private fun initAdapter() {
        //  mBinding.layoutVideoViewpager.offscreenPageLimit = 3
        val adapter = BaseFragmentPagerAdapter(activity!!.supportFragmentManager, mFragment, mTitle)
        mBinding.layoutVideoViewpager.adapter = adapter
        mBinding.layoutVideoTab.setupWithViewPager(mBinding.layoutVideoViewpager)
        mBinding.layoutVideoViewpager.addOnPageChangeListener(
            TabLayout.TabLayoutOnPageChangeListener(
                mBinding.layoutVideoTab
            )
        )
    }

    override fun onPause() {
        super.onPause()
        JCVideoPlayer.releaseAllVideos()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        JCVideoPlayerManager.completeAll()
    }
}